from PySide6.QtWidgets import QApplication, QMessageBox, QGraphicsScene
from PySide6.QtUiTools import QUiLoader
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import pandas as pd

import demo2
import demo3

class MyFigureCanvas(FigureCanvas):
    """
    通过继承FigureCanvas类，使得该类既是一个Pyside的Qwidget，又是一个matplotlib的FigureCanvas，这是连接pyqt5与matplotlib的关键
    """

    def __init__(self, parent=None):
        self.figure = plt.Figure(tight_layout=True)  # tight_layout: 用于去除画图时两边的空白
        FigureCanvas.__init__(self, self.figure)  # 初始化父类
        self.setParent(parent)

        self.fig1 = self.figure.add_subplot(121)  # 添加子图 #两行两列第一个
        self.fig2 = self.figure.add_subplot(122)  # 添加子图函数，三个参数分别表示纵向分几个子图，横向分几个子图，当前为第几个子图
        #self.fig3 = self.figure.add_subplot(133)



class main_win:
    def __init__(self):
        self.ui = QUiLoader().load('ui_code/main.ui')
        self.gv_visual_data_content = MyFigureCanvas()

        demo2.piaofang_movie()
        demo3.main_movie()

        self.main()


    def piaofang(self,data):
        #a,b=data.shape#行数、列数
        typelist=data[2]
        typedict=dict()
        for i in typelist:
            # print(i)
            typedict[i]=typedict.get(i,0)+1#{'剧情': 11, '喜剧': 4, '动作': 5, '奇幻': 1, '爱情': 1, '动画': 3}
        colors=['lightskyblue','silver','yellow','salmon','grey','lime','gold','red','green','pink']
        self.gv_visual_data_content.fig1.pie(typedict.values(), labels=typedict.keys(), autopct='%1.1f%%', colors=colors[0:len(typedict)])
        plt.title("2021中国电影票房榜电影类型", fontsize=24)
        self.gv_visual_data_content.fig1.legend()  # 显示线条信息

    def price_month(self,data):
        Xplot_title = '月份'
        Yplot_title = '总票房/万'
        Plot_title = '票房上映时间关系图'
        pricelist=data[3]
        #print(pricelist)
        time=data[8]
        month_list=[]
        # print(time)
        for I in time:
            month=I.split('-')[1]
            month_list.append(int(month))
        price_ave=int(sum(pricelist)/25)

        self.gv_visual_data_content.fig2.plot([price_ave,price_ave],[1,12],color='blue',linestyle="--",label='平均票房')
        self.gv_visual_data_content.fig2.scatter(pricelist,month_list,color='green',label='票房')
        self.gv_visual_data_content.fig2.axis([0,600,1,13])
        self.gv_visual_data_content.fig2.set_xlabel(Yplot_title, fontsize=12)
        self.gv_visual_data_content.fig2.set_ylabel(Xplot_title, fontsize=12)
        self.gv_visual_data_content.fig2.set_title(Plot_title, fontsize=12)  # 标题
        self.gv_visual_data_content.fig2.legend( loc=0,ncol=3, mode="expand", borderaxespad=0.)  # 显示线条信息

    def Text1(self,data):
        # for i in date:
        #     print(i)
        #print(date[0:1])
        # for i in range(25):
        data=data[0:12]
        self.ui.label_2.setText(str(data[1][0])+'                '+'票房:'+str(data[3][0])+'万'+'\n'+str(data[1][1])+'      '+'票房:'+str(data[3][1])+'万'+'\n'+str(data[1][2])+'       '+'票房:'+str(data[3][2])+'万'
                                +'\n'+str(data[1][3])+'      '+'票房:'+str(data[3][3])+'万'+'\n'+str(data[1][4])+'       '+'票房:'+str(data[3][4])+'万'+'\n'+str(data[1][5])+'           '+'票房:'+str(data[3][5])+'万'
                                +'\n'+str(data[1][6])+'            '+'票房:'+str(data[3][6])+'万'+'\n'+str(data[1][7])+'   '+'票房:'+str(data[3][7])+'万'+'\n'+str(data[1][8])+'   '+'票房:'+str(data[3][8])+'万'
                                +'\n'+str(data[1][9])+'             '+'票房:'+str(data[3][9])+'万'+'\n'+str(data[1][10])+'          '+'票房:'+str(data[3][10])+'万')
    def Text2(self,data):
        self.ui.label_3.setText(str(data['1'][0])+'\n'+str(data['1'][2])+'\n'+str(data['1'][4])+'\n'+str(data['1'][6])+'\n'+str(data['1'][8])
                                +'\n'+str(data['1'][10])+'\n'+str(data['1'][12])+'\n'+str(data['1'][14])+'\n'+str(data['1'][16])+'\n'+str(data['1'][18]))
        #self.ui.label.setText(str(data['1'][2]))

    def Text3(self,data):
        #self.ui.label_1.setText(str(data))
        self.ui.label_1.setText('雄狮少年'+'\n'+str(data[0][1])+'\n'+str(data[0][2])+'\n'+str(data[0][3])+'\n'+str(data[0][4])+'\n'+str(data[0][5])+'\n'+str(data[0][6])+'\n'+str(data[0][7])+'\n'+str(data[0][8]))

    def Text4(self,data):
        self.ui.label_7.setWordWrap(True)
        self.ui.label_7.setText(str(data['评论'][0])+'\n'+'\n'+str(data['评论'][1])+'\n'+'\n'+str(data['评论'][2]))
        #print(str(data['评论'][0]))
    def main(self):
        plt.rcParams['font.sans-serif'] = ['SimHei']
        plt.rcParams['axes.unicode_minus'] = False

        data1=pd.read_csv('./movies.csv',sep=',',header=None)
        self.piaofang(data1)
        self.price_month(data1)
        self.Text1(data1)

        data2=pd.read_csv('./10_movie.csv',sep='\n')
        self.Text2(data2)

        data3=pd.read_csv('./movie_des.csv',sep='\n',header=None,names=[0])
        self.Text3(data3)

        data4=pd.read_csv('./comment.csv',sep='\n',header=None,names=['评论'])
        self.Text4(data4)

        self.graphic_scene = QGraphicsScene()
        self.graphic_scene.addWidget(self.gv_visual_data_content)
        self.ui.graphicsView.setScene(self.graphic_scene)  # 把QGraphicsScene放入QGraphicsView
        self.ui.graphicsView.show()

if __name__ == "__main__":
    app = QApplication([])
    main_win1 = main_win()
    main_win1.ui.show()
    app.exec()
